<?php

class Price_model extends CI_Model
{
	function GetPrices($PriceClass, $CarId)
	{	
		
		
		$sqlCommand='SELECT * FROM bilpris WHERE BilID = ? AND PrisklassID = ?';
		$query=$this->db->query($sqlCommand,array($CarId,$PriceClass));
		
		
		if ($query->num_rows()==1)
		{	

			foreach ($query->result_array() as $row)
			{
			   $WeekPrice = $row['VeckoPris'];
			   $DayPrice = $row['DagPris'];
			   $HourPrice = $row['TimmarPris'];
			}
			
			$PriceArray = array('WeekPrice' => $WeekPrice, 'DayPrice' => $DayPrice, 'HourPrice' => $HourPrice);
			
			return $PriceArray;
		}
		
		else
		{
			return FALSE;
		}
	}
	
	
	
	
	function CheckDiscount($code)
	{
	
		$CodeID = $this->GetDiscountID($code);
		
		
		$sqlCommandTimeDisc='SELECT * FROM periodsrabatt WHERE RabattID = ?';
		$query=$this->db->query($sqlCommandTimeDisc,array($CodeID));
		
		$x = $query->num_rows();
		
		//Om det är en Periodsrabatt
		if($x != 0)
		{
			//Dagens Datum
			$today = now();
			
			   $row = $query->row_array();
				

				$s = $row['StartDatum'];
				$e = $row['SlutDatum'];
   			   $sdate = human_to_unix($s . " 00:01:00");
			   $edate = human_to_unix($e . " 23:59:00");
			   
			   if ($today >= $sdate && $today <= $edate)
			   {
				   	
					$sqlCommandDisc='SELECT * FROM rabatt WHERE ID = ?';
					$queryDisc=$this->db->query($sqlCommandDisc,array($CodeID));
				
				    $Discrow = $queryDisc->row_array();
	
				    $d = $Discrow['rabatt'];
					
					return $d;
			   }
			   
			   else
			   {
			   	
				return 0;
				
			   }
	
				
		}
		
		
		
		$sqlCommandOneUseDisc='SELECT * FROM oneuserabatt WHERE RabattID = ?';
		$query2=$this->db->query($sqlCommandOneUseDisc,array($CodeID));
		
		$y = $query2->num_rows();
		
		//Om det är en engångsrabatt
		
		if ($y != 0)
		{
				//Kollar om rabatten är annvänd eller inte
			  $row = $query2->row_array();

   			  $used = $row['annvand'];
			
			
			if($used == 0)
			{
				
					$sqlCommandOneDisc='SELECT * FROM rabatt WHERE ID = ?';
					$queryOneDisc=$this->db->query($sqlCommandOneDisc,array($CodeID));
				
				    $Discrow = $queryOneDisc->row_array();
	
				    return $Discrow['rabatt'];
				
				
			}
			
			else
			{
				return 0;
			}
		}
		
		else
		{
			
			return 0;
		}
		
		
	}
	
	
	function GetDiscountID($code)
	{
			// Hämtar IDt för robatten med koden
			$sqlCommandGetID='SELECT * FROM rabatt WHERE kod = ?';
			$queryGetID=$this->db->query($sqlCommandGetID,array($code));
		
			$x = $queryGetID->num_rows();
			
			if($x != 0)
			{
				$row = $queryGetID->row_array();
	
				$id = $row['ID'];
			
				return $id;
			}
			
			else
			{
				return 0;
			}
			
	}
	
}
	